package com.elm.mapper;

import com.elm.po.DeliveryAddress;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

@Mapper
public interface DeliveryAddressMapper {
	@Select("select * from delivery_address where user_id=#{userId} And is_deleted=0")
    List<DeliveryAddress> getDeliveryAddressByUserId(int userId);
    
    @Insert("INSERT INTO delivery_address (id,is_deleted, address, contact_name, contact_sex,contact_tel,user_id,is_default) " +
            "VALUES (#{id},0, #{address}, #{contactName}, #{contactSex}, #{contactTel},#{userId},0)")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int saveDeliveryAddress(DeliveryAddress deliveryAddress);
    
    @Select("Select * from delivery_address where id=#{id}")
    DeliveryAddress getDeliveryAddressById(int id);
    
//    @Update("update delivery_address set address=#{address},contact_name=#{contactName},contact_sex=#{contactSex},contact_tel=#{contactTel} where id=#{id}")
//    int updateDeliveryAddress(DeliveryAddress deliveryAddress);
    
    @Delete("update  delivery_address set is_deleted=1 where id=#{daId}")
    int removeDeliveryAddress(Integer daId);
    
    @Update("update delivery_address set is_default=1 where id=#{daId} ")
    int setDefaultAddress(Integer daId);
    
    @Update("UPDATE delivery_address SET is_default = 0 WHERE user_id = #{userId}")
    void resetUserDefaultAddress(int userId);
    
    @Update("UPDATE delivery_address SET is_deleted = 1 WHERE id = #{id}")
    void updateAddressStatus(int id);
}